import { Alert, Flex, View, Badge, Link } from '@aws-amplify/ui-react'; import { Example, ExampleCode } from '@/components/Example'; import { ComponentStyleDisplay } from '@/components/ComponentStyleDisplay'; import ThemeExample from '@/components/ThemeExample.mdx'; import { AlertDemo } from './demo'; import { AccessibleAlert, DefaultAlertExample, AlertVariationsExample, AlertHeadingExample, AlertIconExample, DismissibleAlertExample, OnDismissAlertExample, AlertStylePropsExample, AlertThemeExample, DismissButtonLabelExample, RoleOverride } from './examples'; Usage note: The Alert component has an ARIA `alert` role by default which has some accessibility implications. ## Demo ## Usage Import the Alert component and styles. ```jsx file=./examples/DefaultAlertExample.tsx ``` ### Variations Use the `variation` prop to change the Alert variation. Available options are `info`, `error`, `warning`, `success`, and none (default). ```jsx file=./examples/AlertVariationsExample.tsx ``` ### Heading Use the `heading` prop to pass a heading to the Alert. ```jsx file=./examples/AlertHeadingExample.tsx ``` ### Icon Use the `hasIcon` prop to change whether the Alert includes an icon. Defaults to `true` (includes icon). ```jsx file=./examples/AlertIconExample.tsx ``` ### Dismissible Use the `isDismissible` prop to control whether the user can dismiss the Alert. Defaults to `false` (not dismissible). ```jsx file=./examples/DismissibleAlertExample.tsx ``` ### onDismiss Use the `onDismiss` prop to pass a function that will run when the Alert is dismissed. Note that `isDismissible` must be set to `true`. ```jsx file=./examples/OnDismissAlertExample.tsx ``` ## CSS Styling ```tsx file=./examples/AlertThemeExample.tsx ``` ### Target classes ### Global styling To override styling on all Alerts, you can set the Amplify CSS variables or use the built-in `.amplify-alert` class. Change the default Alert to yellow ```css /* styles.css */ :root { --amplify-components-alert-background-color: yellow; } /* OR */ .amplify-alert { background-color: yellow; } ``` To replace the Alert styling, unset it: ```css .amplify-alert { all: unset; /* Add your styling here*/ } ``` ### Local styling To override styling on a specific Alert, you can use (in order of increasing specificity): a class selector, data attributes, or style props. _Using a class selector:_ This is a purple Alert ```css /* styles.css */ .purple-alert { color: white; background-color: rebeccapurple; } ``` ```jsx import './styles.css'; This is a purple Alert ; ``` _Using data attributes:_ Red Alert! Default Alert styling unaffected ```css /* styles.css */ /* Override only error variation styles */ .amplify-alert[data-variation='error'] { color: white; background-color: crimson; } ``` ```jsx import './styles.css'; Red Alert! Default Alert styling unaffected ``` _Using style props:_ ```jsx file=./examples/AlertStylePropsExample.tsx ``` ## Accessibility The Alert component in Amplify UI has the `alert` role by default. The `alert` role is an assertive live region which means any changes to the content of the Alert or adding the Alert dynamically to the DOM will cause the Alert to be announced by a screen reader. **This can be disruptive** to screen reader users, so it is best used sparingly and only when the Alert content requires the user's immediate attention. Please see the ARIA Authoring Practices Guide for the `alert` role for more information and use cases. ### Dynamic Alert The following code shows an example of dynamically displaying an Alert. A screenreader will announce the content of the Alert when it is visible. ```jsx file=./examples/AccessibleAlert.tsx ``` ### Role override If you're displaying information that isn't critical or time sensitive, and only want the visual style of the Alert component without the accessibility side effects, you can override the role attribute like in the following example: ```jsx file=./examples/RoleOverride.tsx ``` ### Custom aria label You can configure a custom `aria-label` for the dismiss button using the `dismissButtonLabel` prop (defaults to `'Dismiss alert'`). ```jsx file=./examples/DismissButtonLabelExample.tsx ```